package com.example.anql.controller;

import com.example.anql.common.ReturnObject;
import com.example.anql.entity.UserInfo;
import com.example.anql.service.UserInfoService;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

/**
 * (UserInfo)表控制层
 *
 * @author makejava
 * @since 2022-12-10 12:35:17
 */
@RestController
@RequestMapping("/userInfo")
public class UserInfoController {
    /**
     * 服务对象
     */
    @Resource
    private UserInfoService userInfoService;



    // 登录 # 增加返回用户信息
    @PostMapping("/login")
    public Object ReturnObject(@RequestBody  UserInfo userInfo, HttpSession session ) {
        UserInfo userInfos = userInfoService.login(userInfo.getAccount(), userInfo.getPasswd());
        session.setAttribute("loginUser", userInfos);


        if(userInfos != null) {
            return ResponseEntity.ok(userInfos );
        }else {
            return "用户名密码不正确";
        }

    }

    // 登出
    @PostMapping("/logout")
    public int logout(    HttpSession session ) {
        session.invalidate();
        return 1;
    }



    /**
     * 分页查询
     *
     * @param userInfo 筛选条件
     * @return 查询结果
     */
    @PostMapping("/queryByPage")
    public ReturnObject queryByPage(@RequestBody  UserInfo userInfo ) {

        return userInfoService.queryByPage(userInfo);
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @PostMapping("/queryById")
    public ResponseEntity<UserInfo> queryById(@RequestBody  UserInfo userInfo) {
        return ResponseEntity.ok(this.userInfoService.queryById(userInfo.getId()));
    }

    /**
     * 新增数据
     *
     * @param userInfo 实体
     * @return 新增结果
     */
    @PostMapping("/add")
    public ResponseEntity<UserInfo> add(@RequestBody UserInfo userInfo) {
        System.out.println(userInfo.getId());
        System.out.println(userInfo.getAccount());
        System.out.println(userInfo.getVip());
        return ResponseEntity.ok(this.userInfoService.insert(userInfo));
    }

    /**
     * 编辑数据
     *
     * @param userInfo 实体
     * @return 编辑结果
     */
    @PostMapping("/edit")
    public ResponseEntity<UserInfo> edit(@RequestBody UserInfo userInfo) {
        return ResponseEntity.ok(this.userInfoService.update(userInfo));
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @PostMapping("/deleteById")
    public ResponseEntity<Boolean> deleteById(@RequestBody UserInfo userInfo) {
        return ResponseEntity.ok(this.userInfoService.deleteById(userInfo.getId()));
    }





}

